#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\icon.ico
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Fileversion=0.0.1.0
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_Language=1040
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_After=move "settings.exe" "..\MLSettings.exe"
#AutoIt3Wrapper_Run_After=move "settings_x64.exe" "..\MLSettings_x64.exe"
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <Misc.au3>

#include "graphic.au3"
#include "metrocontrol.au3"
#include "common.au3"

Opt("GUIOnEventMode", 1)

Global $aControl[8]

If WinExists("Metro Launch") Then _Settings_Activate()

Func _Settings_Activate($hParentGUI = "")
	_LoadTranslation(IniRead(@ScriptDir & "\settings.ini", "general", "lang", "English.lng"))
	_GDIPlus_Startup()
	Global $aMsgText[2] = [$aLang[16], $aLang[17]]
	If IsHWnd($hParentGUI) Then
		Global $hSettingsGUI = GUICreate("Metro Launch Settings", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, Default, $hParentGUI)
	Else
		Global $hSettingsGUI = GUICreate("Metro Launch Settings", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
	EndIf
	Global $hSettingsGraphics = _GDIPlus_GraphicsCreateFromHWND($hSettingsGUI)
	GUISetBkColor(0xffffff)
	$aControl[0] = _MetroUI_CreateLabel($hSettingsGUI, $aLang[18], 40, 40, 200, 128, 42, 0xacacac)
	GUISetOnEvent($GUI_EVENT_CLOSE, "_Settings_Exit")
	$aControl[1] = GUICtrlCreateLabel("", 80, @DesktopHeight / 4, 128, 48)
	GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	GUICtrlSetOnEvent(-1, "_GeneralTab")
	$aControl[2] = GUICtrlCreateLabel("", 80, @DesktopHeight / 4 + 64, 128, 48)
	GUICtrlSetOnEvent(-1, "_AboutTab")
	GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	GUISetState(@SW_SHOW)
	_GUI_MoveFull($hSettingsGUI, 3)
	_GeneralTab()

	While 1
		Sleep(10)
	WEnd
EndFunc   ;==>_Settings_Activate

Func _Settings_Exit()
	Local $aWinPos = WinGetPos($hSettingsGUI)
	For $i = 0 To @DesktopWidth - $aWinPos[2] / 700 Step 30
		WinMove($hSettingsGUI, "", $i, 0)
		Sleep(10)
	Next
	For $i2 = @DesktopWidth - $aWinPos[2] / 700 To @DesktopWidth Step 10
		WinMove($hSettingsGUI, "", $i2, 0)
		Sleep(10)
	Next
	_GDIPlus_Shutdown()
	Exit
EndFunc   ;==>_Settings_Exit

Func _GeneralTab()
	For $i = $aControl[3] To $aControl[4] Step 1
		GUICtrlDelete($i)
	Next
	_WinAPI_RedrawWindow($hSettingsGUI, "", "", BitOR($RDW_ERASE, $RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME, $RDW_ALLCHILDREN))
	$aControl[3] = GUICtrlCreateDummy()
	_MetroUI_CreateButton($hSettingsGUI, $aLang[24], @DesktopWidth / 8 * 7, @DesktopHeight / 8 * 7, 128, 32)
	GUICtrlSetOnEvent(-1, "_OK")
	_MetroUI_CreateButton($hSettingsGUI, $aLang[25], @DesktopWidth / 8 * 7 - 144, @DesktopHeight / 8 * 7, 128, 32)
	GUICtrlSetOnEvent(-1, "_Settings_Exit")
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[18], 40, 40, 200, 128, 42, 0xacacac)
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[19], @DesktopWidth / 4, @DesktopHeight / 16 * 3, 180, 48, 24, 0xacacac)
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[20], @DesktopWidth / 4 + 64, @DesktopHeight / 4, 180, 48, 16)
	$aControl[5] = _MetroUI_CreateInput($hSettingsGUI, @DesktopWidth / 4 + 64, @DesktopHeight / 4 + 32, 256, 32)
	GUICtrlSetData(-1, StringReplace(IniRead(@ScriptDir & "\settings.ini", "general", "background", ""), "0x", "#"))
	_MetroUI_CreateButton($hSettingsGUI, $aLang[22], @DesktopWidth / 4 + 64 + 320, @DesktopHeight / 4 + 32, 128, 32)
	GUICtrlSetOnEvent(-1, "_GeneralBrowse")
	_MetroUI_CreateButton($hSettingsGUI, $aLang[23], @DesktopWidth / 4 + 64 + 320 + 144, @DesktopHeight / 4 + 32, 128, 32)
	GUICtrlSetOnEvent(-1, "_GeneralColor")
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[21], @DesktopWidth / 4 + 64, @DesktopHeight / 8 * 3, 180, 48, 16)
	$aControl[6] = _MetroUI_CreateInput($hSettingsGUI, @DesktopWidth / 4 + 64, @DesktopHeight / 8 * 3 + 32, 256, 32)
	GUICtrlSetData(-1, StringReplace(IniRead(@ScriptDir & "\settings.ini", "lockscreen", "background", ""), "0x", "#"))
	_MetroUI_CreateButton($hSettingsGUI, $aLang[22], @DesktopWidth / 4 + 64 + 320, @DesktopHeight / 8 * 3 + 32, 128, 32)
	GUICtrlSetOnEvent(-1, "_LSBrowse")
	_MetroUI_CreateButton($hSettingsGUI, $aLang[23], @DesktopWidth / 4 + 64 + 320 + 144, @DesktopHeight / 8 * 3 + 32, 128, 32)
	GUICtrlSetOnEvent(-1, "_LSColor")
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[52], @DesktopWidth / 4, @DesktopHeight / 2, 180, 48, 24, 0xacacac)
	Local $sLangList = ""
	Local $hFFF = FileFindFirstFile(@ScriptDir & "\lang\*.lng")
	While 1
		Local $hFFN = FileFindNextFile($hFFF)
		If @error Then ExitLoop
		Local $sLangName = IniRead(@ScriptDir & "\lang\" & $hFFN, "info", "language", "Error")
		Local $sLangAuthor = IniRead(@ScriptDir & "\lang\" & $hFFN, "info", "author", "Error")
		If $sLangName <> "Error" And $sLangAuthor <> "Error" Then
			$sLangList &= $sLangName & " (" & $sLangAuthor & ")|"
		EndIf
	WEnd
	FileClose($hFFF)
	$sLangList = StringLeft($sLangList, StringLen($sLangList) - 1)
	Local $sLang = IniRead(@ScriptDir & "\settings.ini", "general", "lang", "English.lng")
	Local $sLangName = IniRead(@ScriptDir & "\lang\" & $sLang, "info", "language", "Error")
	Local $sLangAuthor = IniRead(@ScriptDir & "\lang\" & $sLang, "info", "author", "Error")
	$aControl[7] = GUICtrlCreateCombo("", @DesktopWidth / 4 + 64, @DesktopHeight / 16 * 9 + 20, 180)
	GUICtrlSetData(-1, $sLangList, $sLangName & " (" & $sLangAuthor & ")")
	$aControl[4] = GUICtrlCreateDummy()
	_GeneralHighlight()
EndFunc   ;==>_GeneralTab

Func _AboutTab()
	For $i = $aControl[3] To $aControl[4] Step 1
		GUICtrlDelete($i)
	Next
	$aControl[3] = GUICtrlCreateDummy()
	_WinAPI_RedrawWindow($hSettingsGUI, "", "", BitOR($RDW_ERASE, $RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME, $RDW_ALLCHILDREN))
	_MetroUI_CreateButton($hSettingsGUI, $aLang[25], @DesktopWidth / 8 * 7 - 144, @DesktopHeight / 8 * 7, 128, 32)
	GUICtrlSetOnEvent(-1, "_Settings_Exit")
	_MetroUI_CreateLabel($hSettingsGUI, $aLang[46], @DesktopWidth / 4, @DesktopHeight / 16 * 3, 240, 48, 24, 0xacacac)
	_MetroUI_CreateLabel($hSettingsGUI, "The Metro Launch Team", @DesktopWidth / 4 + 64, @DesktopHeight / 4, 300, 48, 16)
	_MetroUI_CreateLabel($hSettingsGUI, "Project manager and main code: FrontBack", @DesktopWidth / 4 + 64, @DesktopHeight / 4 + 32, 300, 48, 12)
	_MetroUI_CreateLabel($hSettingsGUI, "Special Thanks to", @DesktopWidth / 4 + 64, @DesktopHeight / 8 * 3, 300, 48, 16)
	_MetroUI_CreateLabel($hSettingsGUI, "Icons taken from http://metro.windowswiki.info/mi/ and from http://www.iconkingdom.com/." & @CRLF & "This software is under GNU/GPL v2." & @CRLF & "Help and support (IT): http://autoit.forumcommunity.net." & @CRLF & "Compiled with AutoIt v3.3.8.1.", @DesktopWidth / 4 + 64, @DesktopHeight / 8 * 3 + 32, @DesktopWidth - (@DesktopWidth / 4 + 64), 300, 12)
	$aControl[4] = GUICtrlCreateDummy()
	_AboutHighlight()
EndFunc   ;==>_AboutTab

Func _GeneralHighlight()
	Local $hBrushSelected = _GDIPlus_BrushCreateSolid(0xff6699cc)
	Local $hBrushUnselected = _GDIPlus_BrushCreateSolid(0xffe4e4e4)
	Local $hTextBrush = _GDIPlus_BrushCreateSolid(0xffffffff)
	Local $hFormat = _GDIPlus_StringFormatCreate()
	Local $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Semilight")
	Local $hFont = _GDIPlus_FontCreate($hFamily, 24, 0, 2)

	Local $tLayout = _GDIPlus_RectFCreate(90, @DesktopHeight / 4 + 6, 128, 48)
	_GDIPlus_GraphicsFillRect($hSettingsGraphics, 80, @DesktopHeight / 4, 128, 48, $hBrushSelected)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hSettingsGraphics, $aMsgText[0], $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hSettingsGraphics, $aMsgText[0], $hFont, $aInfo[0], $hFormat, $hTextBrush)

	Local $tLayout = _GDIPlus_RectFCreate(90, @DesktopHeight / 4 + 64 + 6, 128, 48)
	_GDIPlus_GraphicsFillRect($hSettingsGraphics, 80, @DesktopHeight / 4 + 64, 128, 48, $hBrushUnselected)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hSettingsGraphics, $aMsgText[1], $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hSettingsGraphics, $aMsgText[1], $hFont, $aInfo[0], $hFormat, $hTextBrush)
EndFunc   ;==>_GeneralHighlight

Func _AboutHighlight()
	Local $hBrushSelected = _GDIPlus_BrushCreateSolid(0xff6699cc)
	Local $hBrushUnselected = _GDIPlus_BrushCreateSolid(0xffe4e4e4)
	Local $hTextBrush = _GDIPlus_BrushCreateSolid(0xffffffff)
	Local $hFormat = _GDIPlus_StringFormatCreate()
	Local $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Semilight")
	Local $hFont = _GDIPlus_FontCreate($hFamily, 24, 0, 2)

	Local $tLayout = _GDIPlus_RectFCreate(90, @DesktopHeight / 4 + 6, 128, 48)
	_GDIPlus_GraphicsFillRect($hSettingsGraphics, 80, @DesktopHeight / 4, 128, 48, $hBrushUnselected)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hSettingsGraphics, $aMsgText[0], $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hSettingsGraphics, $aMsgText[0], $hFont, $aInfo[0], $hFormat, $hTextBrush)

	Local $tLayout = _GDIPlus_RectFCreate(90, @DesktopHeight / 4 + 64 + 6, 128, 48)
	_GDIPlus_GraphicsFillRect($hSettingsGraphics, 80, @DesktopHeight / 4 + 64, 128, 48, $hBrushSelected)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hSettingsGraphics, $aMsgText[1], $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hSettingsGraphics, $aMsgText[1], $hFont, $aInfo[0], $hFormat, $hTextBrush)
EndFunc   ;==>_AboutHighlight

Func _OK()
	IniWrite(@ScriptDir & "\settings.ini", "general", "background", StringReplace(GUICtrlRead($aControl[5]), "#", "0x"))
	Local $hFFF = FileFindFirstFile(@ScriptDir & "\lang\*.lng")
	While 1
		Local $hFFN = FileFindNextFile($hFFF)
		If @error Then ExitLoop
		Local $sLangName = IniRead(@ScriptDir & "\lang\" & $hFFN, "info", "language", "Error")
		Local $sLangAuthor = IniRead(@ScriptDir & "\lang\" & $hFFN, "info", "author", "Error")
		If GUICtrlRead($aControl[7]) = $sLangName & " (" & $sLangAuthor & ")" Then
			IniWrite(@ScriptDir & "\settings.ini", "general", "lang", $hFFN)
		EndIf
	WEnd
	FileClose($hFFF)
	IniWrite(@ScriptDir & "\settings.ini", "lockscreen", "background", StringReplace(GUICtrlRead($aControl[6]), "#", "0x"))
	_Settings_Exit()
EndFunc   ;==>_OK

Func _GeneralColor()
	Local $sColor = _ChooseColor(2)
	If $sColor <> -1 Then
		GUICtrlSetData($aControl[5], StringReplace($sColor, "0x", "#"))
	EndIf
EndFunc   ;==>_GeneralColor

Func _GeneralBrowse()
	Local $sOpen = FileOpenDialog($aLang[14], @DesktopDir, $aLang[15] & " (*.jpg;*.bmp;*.gif;*.png)", 1 + 2)
	If Not @error Then
		GUICtrlSetData($aControl[5], $sOpen)
	EndIf
EndFunc   ;==>_GeneralBrowse

Func _LSColor()
	Local $sColor = _ChooseColor(2)
	If $sColor <> -1 Then
		GUICtrlSetData($aControl[6], StringReplace($sColor, "0x", "#"))
	EndIf
EndFunc   ;==>_LSColor

Func _LSBrowse()
	Local $sOpen = FileOpenDialog($aLang[14], @DesktopDir, $aLang[15] & " (*.jpg;*.bmp;*.gif;*.png)", 1 + 2)
	If Not @error Then
		GUICtrlSetData($aControl[6], $sOpen)
	EndIf
EndFunc   ;==>_LSBrowse
